package com.example.springs;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

//磁珠劫必须加，项目组使用xml配置的
@Component
public class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtil.context=applicationContext;
    }

    public static  ApplicationContext getContext() {
        return context;
    }

    public static Object getBean(String name) {
        return getContext().getBean(name);
    }
    public static <T> T getBean(Class<T> type){
        return getContext().getBean(type);
    }
}
